package Array;

import java.util.Arrays;

public class MyArray implements SeqList{

    //实际存储元素的数组
    private int[] elementData;
    //当前动态数组中存储的实际元素
    private int size;
    //默认数组长度
    private static final int DEFAULT_CAPACITY = 10;

    MyArray(){
        this(DEFAULT_CAPACITY);
    }

    MyArray(int capacity){
        this.elementData = new int[capacity];
    }

    @Override
    public void add(int val) {
        if(elementData.length==0){
            throw new IllegalArgumentException("arr length can not be zero!");
        }
        if(size==this.elementData.length){
            grow();
        }
        //尾插就是将元素插在当前有效元素的下一个位置
        //而size恰好就是当前有效元素的下一个位置
        this.elementData[size] = val;
        size++;

    }

    private void grow() {
        int oldLength = this.elementData.length;
        int newLength =0;
        if(oldLength==1){//1的1.5倍还是1所有特殊处理
            newLength = oldLength+1;
        } else {
            newLength = oldLength + (oldLength >> 1);
        }

        this.elementData = Arrays.copyOf(this.elementData,newLength);
    }

    @Override
    public void add(int index, int val) {
        if(index<0||index>size){
            throw new IllegalArgumentException("add illegal index");
        }
        if(index==size){
            this.add(val);
            return;
        }
        if(size==this.elementData.length){
            grow();
        }
        for (int i = size-1; i >=index ; i--) {
            this.elementData[i+1] = this.elementData[i];
        }
        this.elementData[index] = val;
        size++;

    }

    @Override
    public boolean contains(int val) {
        for (int i = 0; i < size ; i++) {
            if(this.elementData[i]==val){
                return true;
            }
        }
        return false;
    }

    @Override
    public int get(int index) {
        rangeCheck(index);
        return this.elementData[index];
    }

    private void rangeCheck(int index) {
        if(index<0||index>=size){
            throw new IllegalArgumentException("illegal index");
        }
    }

    @Override
    public int set(int index, int newVal) {
        rangeCheck(index);
        int tmp = this.elementData[index];
        this.elementData[index] = newVal;
        return tmp;
    }

    @Override
    public int removeByIndex(int index) {
        rangeCheck(index);
        int tmp = this.elementData[index];
        for (int i = index; i <size-1 ; i++) {
            this.elementData[i] = this.elementData[i+1];
        }
        size--;
        return tmp;
    }

    @Override
    public void removeByValOnce(int val) {
        for (int i = 0; i < size; i++) {
            if(this.elementData[i]==val){
                removeByIndex(i);
                return;
            }
        }
        System.err.println("当前集合中不存在元素"+val);
    }

    @Override
    public void removeByValAll(int val) {
        boolean flg = false;
        for (int i = 0; i < size; i++) {
            while (i<size&&this.elementData[i]==val){
                removeByIndex(i);
                flg = true;
            }
        }
        if(flg==false){
            System.err.println("当前集合中不存在元素"+val);
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("[");
        for (int i = 0; i < size; i++) {
            sb.append(this.elementData[i]);
            if(i<size-1){
                sb.append(", ");
            }
        }
        sb.append("]");
        return sb.toString();
    }
}
